using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using StockCrawlerLib.DAL;
using DevExpress.Web;
using DevExpress.Data;

namespace StockCrawler {
    public partial class _Default : System.Web.UI.Page {
        private HistoryRepository _hisRepo = new HistoryRepository();
        private StockRepository _stockRepo = new StockRepository();
        
        protected void Page_Load(object sender, EventArgs e) {
            if (!this.IsPostBack)
            {
                //InitMasterGrid();
            }

        }

        private void InitMasterGrid()
        {
            ASPxGridView1.DataSource = _stockRepo.GetDefaultStockList();
            ASPxGridView1.PageIndex = 0;
            ASPxGridView1.DataBind();
        }

        protected void ASPxGridView2_CustomCallback(object sender, ASPxGridViewCustomCallbackEventArgs e)
        {
            string[] args = e.Parameters.Split('|');
            if (args == null || args.Length == 0)
                return;

            if (args[0].Equals("INDEX"))
            {
                int id_stock = (int)ASPxGridView1.GetRowValues(Convert.ToInt32(args[1]), new string[] { "id_stock" });
                Session["id_stock"] = id_stock;
                
                ASPxGridView2.DataSource = null;
                var dt = _hisRepo.Select2(id_stock);
                ASPxGridView2.DataSource = dt;
                ASPxGridView2.DataBind();
            }
            else if (args[0].Equals("SORT"))
            {
                int id_stock = (int)Session["id_stock"];
                var dt = _hisRepo.Select2(id_stock);
                ASPxGridView2.DataSource = dt;
                ASPxGridView2.DataBind();
                if (args[1] == "ngay_giao_dich")
                {
                    ColumnSortOrder order = args[2] == "ASC" ? ColumnSortOrder.Ascending : ColumnSortOrder.Descending;
                    ASPxGridView2.SortBy(ASPxGridView2.Columns[args[1]], order);
                }
            }
            else if (args[0].Equals("PAGING"))
            {
                int id_stock = (int)ASPxGridView1.GetRowValues(ASPxGridView1.VisibleStartIndex, new string[] { "id_stock" });
                Session["id_stock"] = id_stock;

                ASPxGridView2.DataSource = null;
                var dt = _hisRepo.Select2(id_stock);
                ASPxGridView2.DataSource = dt;
                ASPxGridView2.DataBind();
            }

        }

        protected void ASPxGridView1_DataBound(object sender, EventArgs e)
        {
            Page.ClientScript.RegisterStartupScript(this.GetType(), "myScript", "getTest();", true);
           
            //ASPxGridView1.Selection.SelectRow(ASPxGridView1.VisibleStartIndex);
            //string func = string.Format("getDefaultIndex({0});", ASPxGridView1.VisibleStartIndex);
            //Page.ClientScript.RegisterStartupScript(this.GetType(), "myScript", func, true);
            
           
        }

        protected void ASPxGridView2_HtmlDataCellPrepared(object sender, ASPxGridViewTableDataCellEventArgs e)
        {
            if (e.DataColumn.FieldName != "thay_doi") return;
            string san = ASPxGridView2.GetRowValues(e.VisibleIndex, new string[] { "stockexchange_name" }).ToString();
            float gia_dong_cua = (float)ASPxGridView2.GetRowValues(e.VisibleIndex, new string[] { "gia_dong_cua" });
            float thay_doi = Convert.ToSingle(e.CellValue);
            float gia_tham_chieu = gia_dong_cua - thay_doi;
            float gia_tran = 10000;
            float gia_san = -10000;
            if (san == "HOSE")
            {
                float temp1 = gia_tham_chieu + gia_tham_chieu * (float)0.07;
                temp1 = temp1 - temp1 % (float)0.1;
                gia_tran = (float)Math.Round((double)temp1, 1);

                float temp2 = gia_tham_chieu - gia_tham_chieu * (float)0.07;
                //temp2 = temp2 - temp2 % (float)0.1;
                //gia_san = (float)Math.Round((double)temp2, 1);
                temp2 = temp2 + (float)0.05;
                gia_san = (float)Math.Round((double)temp2, 1, MidpointRounding.AwayFromZero);
            }
            else if (san == "HAX")
            {
                float temp1 = gia_tham_chieu + gia_tham_chieu * (float)0.1;
                temp1 = temp1 - temp1 % (float)0.1;
                gia_tran = (float)Math.Round((double)temp1, 1);

                float temp2 = gia_tham_chieu - gia_tham_chieu * (float)0.1;
                //temp2 = temp2 - temp2 % (float)0.1;
                //gia_san = (float)Math.Round((double)temp2, 1);
                temp2 = temp2 + (float)0.05;
                
                gia_san = (float)Math.Round((double)temp2, 1, MidpointRounding.AwayFromZero);
            }
            else if (san == "UPCOM")
            {
                float temp1 = gia_tham_chieu + gia_tham_chieu * (float)0.1;
                temp1 = temp1 - temp1 % (float)0.1;
                gia_tran = (float)Math.Round((double)temp1, 1);

                float temp2 = gia_tham_chieu - gia_tham_chieu * (float)0.1;
                temp2 = temp2 - temp2 % (float)0.1;
                gia_san = (float)Math.Round((double)temp2, 1);
            }



            if (thay_doi < 0)
            {
                if (gia_dong_cua == gia_san)
                    e.Cell.ForeColor = System.Drawing.Color.FromArgb(0x39, 0x87, 0xAE);
                else
                    e.Cell.ForeColor = System.Drawing.Color.Red;
            }
            else if (thay_doi > 0)
                if (gia_dong_cua == gia_tran)
                    e.Cell.ForeColor = System.Drawing.Color.FromArgb(0xFF, 0x00, 0xFF);
                else
                    e.Cell.ForeColor = System.Drawing.Color.FromArgb(0x00, 0xA5, 0x00);
            else
            {
                e.Cell.ForeColor = System.Drawing.Color.FromArgb(0xFF, 0x99, 0x00);
            }
        }
    }
}